﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using DQYT.HTDZ.BLL;
using System.Data;
using DQYT.HTDZ.Common;
using DQYT.HTDZ.Model;

namespace SkWeb.Handlers
{
    public partial class SchemeDesignHandler : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string resultJson = string.Empty;

            int pageSize, pageIndex, recordCount;

            string op = Request.QueryString["op"];

            string format = "\"total\":{0}, \"rows\":{1}";

            switch (op)
            {
                case "getschemedesignwell":
                    string wellCode = Uri.UnescapeDataString(Request.Form["jh"]);
                    int.TryParse(Request.Form["rows"], out pageSize);
                    int.TryParse(Request.Form["page"], out pageIndex);
                    if (pageSize == 0) pageSize = 15;
                    if (pageIndex == 0) pageIndex = 1;
                    recordCount = 0;

                    if(!string.IsNullOrWhiteSpace(wellCode))
                    wellCode = wellCode.Replace("'", "").Replace("\\", "").Replace("&", "");

                    SkgysjbBLL skgysjbBLL = new SkgysjbBLL();
                    DataTable table = skgysjbBLL.QuerySchemeDesignWellByWellCode(wellCode, pageSize, pageIndex, out recordCount);
                    resultJson = JsonHelper.ToJson(table);
                    resultJson = "{" + string.Format(format, recordCount,
                        string.IsNullOrWhiteSpace(resultJson) ? "[]" : resultJson) + "}";
                    
                    break;
                case "gettemplatetree":
                    int parentId = 0;
                    int.TryParse(Request.QueryString["pid"], out parentId);
                    
                    TB_Template_Menu_BLL tmpMenuBLL = new TB_Template_Menu_BLL();
                    //DataTable menuTreeTable = tmpMenuBLL.QueryTree(parentId);
                    List<TB_Template_Menu_Model> modelList = tmpMenuBLL.QueryTreeList(parentId);
                    recordCount = 0;
                    if (modelList.Count > 0)
                        recordCount = modelList.Count;
                    resultJson = JsonHelper.ToJson(modelList);
                    resultJson = "{" + string.Format(format, recordCount,
                        string.IsNullOrWhiteSpace(resultJson) ? "[]" : resultJson) + "}";
                    break;
                default:
                    break;
            }
            
            Response.ContentType = "text/json";
            Response.Write(resultJson);
        }
    }
}